<template>
  <div class="retrieval-search">
    <div v-for="(v, index) in arr" :key="index">
      <div class="item-li" v-if="hereinafter">
        <!-- <el-icon size="20"><ChatSquare /></el-icon> -->
        <img :src="AvatarHumanBk" alt="" style="width: 20px" />
        <p class="text">{{ v.title }}</p>
      </div>

      <!-- 搜索结果 -->
      <div class="content" v-if="hereinafter">
        <img :src="AvatarCspBk" alt="" style="width: 20px" />
        <p class="concerning">以下是关于{{ v.title }}的搜索结果：</p>
      </div>

      <!-- 工单 -->
      <div class="tickets">
        <div>
          <span class="tickets-title">工单</span>
          <span class="tickets-more" @click="goToWorkOrder(v.title, 'workOrder')">
            <更多>
          </span>
        </div>
        <div v-if="v.ticket.length">
          <template v-for="(item, index) in v.ticket" :key="index">
            <!-- 标题 -->
            <div v-if="item.title" class="tickets-item tickets-item-title">
              ☐
              <span v-dompurify-html="item.title" @click="goToWorkOrderItem('workOrder', item.ticket_number)"></span>
            </div>

            <!-- content -->
            <div class="tickets-item tickets-text" v-if="item.content">
              ● <span v-dompurify-html="item.content"></span>
            </div>

            <div v-for="(i, index) in item?.sub_items" :key="index" class="tickets-item"
              style="padding-bottom: 10px; padding-left: 50px; line-height: 18px">
              ○
              <template v-if="i.title"> <span v-dompurify-html="i.title"></span>： </template>

              <span v-dompurify-html="i.content"></span>
            </div>
          </template>
        </div>

        <div v-else>未查询到相关结果</div>
      </div>

      <!-- 资料 -->
      <!-- <div class="tickets">
      <div @click="goToWorkOrder(v.title, 'document')">
        <span class="tickets-title">资料</span>
        <span class="tickets-more">
          <更多>
        </span>
      </div>

      <div v-if="v.information.length">
        <template v-for="(item, index) in v.information" :key="index">
          <div class="tickets-item-title information-title"
            @click="goToWorkOrderItem('document', item.id, item.template_id)">
            ☐
            <span v-dompurify-html="item.title"> </span>
          </div>

          <template v-for="(i, index) in item?.sub_items" :key="index">
            <div v-if="i.keyword.length" class="tickets-item" style="padding-bottom: 10px; padding-left: 35px">
              ○ <template v-if="i.title"> <span v-dompurify-html="i.title"></span>： </template>
              <span v-for="(k, index) in i.keyword" :key="index" v-dompurify-html="k"
                style="background: #ebb; padding: 3px; box-sizing: border-box; border-radius: 3px"></span>
            </div>

            <div v-else style="padding: 0 0 10px 35px" class="tickets-item">
              ○ <template v-if="i.title"> <span v-dompurify-html="i.title"></span>： </template>
              <span v-dompurify-html="i.content"></span>
            </div>

            <div v-if="i.keyword.length && i.content" class="tickets-item" style="display: flex; padding-left: 45px">
              ●
              <div style="padding-bottom: 10px; padding-left: 5px" v-dompurify-html="i.content"></div>
            </div>
          </template>
        </template>
      </div>

      <div v-else>未查询到相关结果</div>
    </div> -->

      <!-- 视频 -->
      <!-- <div class="tickets">
      <div @click="goToWorkOrder(v.title, 'video')">
        <span class="tickets-title">视频</span>
        <span class="tickets-more">
          <更多>
        </span>
      </div>

      <div v-if="v.video.length">
        <template v-for="(item, index) in v.video" :key="index">
          <div class="tickets-item tickets-item-title" style="padding-top: 10px"
            @click="goToWorkOrderItem('video', item.id)">
            ☐
            <template v-if="item.title"> <span v-dompurify-html="item.title"> </span> </template>
            <span v-for="(i, index) in item?.keyword" :key="index" v-dompurify-html="i" style="
                background: #ebb;
                padding: 3px;
                margin-left: 10px;
                box-sizing: border-box;
                border-radius: 3px;
              "></span>
          </div>
          <div class="tickets-item tickets-text" v-if="item.content">
            ● <span v-dompurify-html="item.content"> </span>
          </div>

          <template v-if="item?.sub_items.length">
            <div v-for="(i, index) in item?.sub_items" :key="index"
              :class="['tickets-item', item.content ? '' : 'tickets-item-top']"
              style="padding-bottom: 10px; padding-left: 35px">
              ○
              <template v-if="i.title"> <span v-dompurify-html="i.title"></span>： </template>

              <span v-dompurify-html="i.content"></span>
            </div>
          </template>
        </template>
      </div>

      <div v-else>未查询到相关结果</div>
    </div> -->
      <div class="tickets" v-if="arr.length && hereinafter">
        <span class="outcome">如果以上没有您想要的结果，可以尝试<span class="create" @click="addWork(v.title)">[创建新工单]</span></span>
      </div>

      <template v-if="newWork">
        <div class="item-li">
          <img :src="AvatarHumanBk" alt="" style="width: 20px" />
          <p class="text">请创建新工单</p>
        </div>

        <div class="content">
          <img :src="AvatarCspBk" alt="" style="width: 20px" />
          <p class="concerning">好的。新工单将在新的选项卡打开</p>
        </div>
      </template>
      <!-- </div> -->
    </div>

    <!-- 更多弹窗 -->
    <van-popup v-model:show="workDialogVisible" position="bottom" :style="{ height: '100%' }" @open="open"
      teleport="body">
      <van-watermark :content="store.AppState.userName" text-color="rgba(0, 0, 0, .1)" :gap-x="80" :gap-y="10" />
      <van-nav-bar title="全部检索结果" left-text="返回" left-arrow @click-left="workDialogVisible = false" />

      <div class="card-list">
        <PaginationList v-slot="{ dataList }" :request-data="requestData" :bottom-position="2" :load-more="2"
          ref="listRef">
          <workOrder :work-params="workParams" :word-list="dataList" />
        </PaginationList>
      </div>
    </van-popup>
  </div>

</template>
<script setup lang="ts">
import { useRouter } from 'vue-router'
import { Result } from '@/utils/request'
import { useUserStore } from '@/stores/user'
import workOrder from '@/components/WorkOrder.vue'
import AvatarCspBk from '@/assets/svg/avatar-csp-bk.svg'
import type { WorkOrderType } from '@/api/worksheets/type'
import AvatarHumanBk from '@/assets/svg/avatar-human-bk.svg'
import PaginationList from '@/components/PaginationList.vue'
import { getSearch, getSearchAllList } from '@/api/worksheets'

const emit = defineEmits<{
  (e: 'search', val: WorkOrderType[]): void
}>()

const router = useRouter()

const store = useUserStore()

const scrollRef = ref()

const newWork = ref(false)

const hereinafter = ref(true)

const workDialogVisible = ref(false)

const arr = ref<WorkOrderType[]>([])

const listRef = ref<InstanceType<typeof PaginationList>>()

const workParams = reactive({
  title: '',
  type: ''
})

//搜索
const searchView = async (val: string, showTile: boolean, domeRef?: any) => {

  store.loading = true

  scrollRef.value = domeRef

  hereinafter.value = showTile

  if (!hereinafter.value) {
    arr.value = []
  }

  try {
    const res: any = await getSearch({ question: val })

    arr.value.push(res.data)

    newWork.value = false

    if (scrollRef.value) {
      // 滚动条回滚到底部
      nextTick(() => {
        let scrollElem = scrollRef.value

        scrollElem?.scrollTo({ top: scrollElem?.scrollHeight, behavior: 'smooth' })
      })
    }

    emit('search', arr.value)
  } catch (err) {
    //
  }
}

// 工单，资料，视频跳转
const goToWorkOrder = (title: string, type: string) => {
  // title：搜索内容  type：类型 workOrder:工单  document:资料 video:视频

  workDialogVisible.value = true

  workParams.title = title

  workParams.type = type
}

// 工单内容跳转
const goToWorkOrderItem = (i: string, id: number, file_id?: number) => {
  // i workOrder:工单  document:资料 video:视频  type:资料类型

  let path = ''

  let paramsList: { id: null | number; material_id: number | null } = {
    id: null,
    material_id: null,
  }

  if (i === 'workOrder') {
    paramsList.id = id

    //删除对象中某个属性
    Reflect.deleteProperty(paramsList, 'material_id')

    path = '/work-order-detail'
  }
  // else if (i === 'document') {
  //   paramsList.id = file_id as number

  //   paramsList.material_id = id

  //   path = '/menu/information'
  // } else {
  //   paramsList.id = id

  //   //删除对象中某个属性
  //   Reflect.deleteProperty(paramsList, 'material_id')

  //   path = '/menu/video'
  // }

  router.push({
    path: path,
    query: { ...paramsList }
  })
}

//点击创建新工单
const addWork = (cont: string) => {
  newWork.value = true

  // 滚动条回滚到底部
  nextTick(() => {
    let scrollElem = scrollRef.value

    scrollElem?.scrollTo({ top: scrollElem?.scrollHeight, behavior: 'smooth' })
  })

  router.push({
    path: '/create-new-work-order',
    query: { title: cont.trim() }
  })
}

// 获取列表数据
const requestItems = (pageIndex: number, pageSize: number): Promise<Result> => {
  return new Promise((resolve, reject) => {
    const params = {
      type: workParams.type,
      question: workParams.title,
      page_number: pageIndex,
      page_size: pageSize
    }

    getSearchAllList(params).then((res: any) => {
      resolve({
        ...res,
        data: res.data.list,
        total: res.count
      })
    }).catch((err) => {
      reject(err)
    });
  })
}

//获取上拉下拉数据
const requestData = (pageIndex: number, pageSize: number) => {
  return requestItems(pageIndex, pageSize)
}

//打开弹窗
const open = () => {
  nextTick(() => {
    listRef.value?.dataInitialization()
  })
}

defineExpose({ searchView })
</script>
<style scoped lang="scss">
.retrieval-search {
  font-size: 12px;
  padding: 0 20px;
  box-sizing: border-box;

  .item-li {
    display: flex;
    align-items: center;
    // margin-top: 20px;

    .text {
      padding: 10px;
      box-sizing: border-box;
      background: #c4c2c2;
      border-radius: 5px;
      margin-left: 10px;
      color: #000;
      font-weight: 600;
      width: 97%;
      max-height: 120px;
      overflow-y: auto;
      word-wrap: break-word;
    }
  }

  .content {
    display: flex;
    align-items: center;
    // margin-top: 20px;

    .concerning {
      width: 97%;
      max-height: 120px;
      overflow-y: auto;
      box-sizing: border-box;
      word-wrap: break-word;
      background: #eeeeee;
      margin-left: 10px;
      padding: 10px;
    }
  }

  .tickets {
    padding-left: 30px;
    padding-bottom: 10px;

    .tickets-title {
      font-size: 14px;
      font-weight: 600;
      margin: 10px 0 5px 0;
      display: inline-block;
      cursor: pointer;
    }

    .tickets-more {
      color: #2d33dd;
      border-bottom: 1px solid #2d33dd;
      cursor: pointer;
      margin-left: 5px;
    }

    .tickets-item {
      padding: 5px 0px 5px 25px;
    }

    .tickets-item-top {
      padding-top: 5px;
    }

    .tickets-item-title span:first-child {
      color: #2d33dd;
      border-bottom: 1px solid #2d33dd;
      cursor: pointer;
    }

    .information-title {
      padding: 0 0 15px 25px;
    }

    .tickets-text {
      padding-top: 10px;
      padding-bottom: 10px;
      padding-left: 50px;
      line-height: 20px;
    }

    .outcome {
      display: inline-block;
      margin-top: 10px;

      .create {
        color: #c23035;
        font-weight: 600;
        padding-left: 10px;
        cursor: pointer;
      }
    }
  }

  .card-list {
    width: 100%;
    height: calc(100vh - 80px);
    overflow: auto;
    -webkit-overflow-scrolling: touch;
    // padding-bottom: 40px;
    padding: 0 20px 40px 20px;
    box-sizing: border-box;
  }
}
</style>
